package com.anxinr.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.anxinr.constant.ShopConstant;
import com.anxinr.enums.OrderEnum;
import com.anxinr.exception.LogicException;
import com.anxinr.mapper.AddressMapper;
import com.anxinr.mapper.CommodityMapper;
import com.anxinr.mapper.OrderGoodsMapper;
import com.anxinr.mapper.OrderMapper;
import com.anxinr.pojo.dto.AddressDTO;
import com.anxinr.pojo.dto.OrderDTO;
import com.anxinr.pojo.entity.Address;
import com.anxinr.pojo.entity.Order;
import com.anxinr.pojo.entity.OrderGoods;
import com.anxinr.service.AddressService;
import com.anxinr.service.OrderService;
import com.anxinr.util.UserInfoContext;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author anxinr
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper ;
    @Autowired
    private OrderGoodsMapper goodsMapper ;
    @Autowired
    private UserInfoContext userInfoContext ;
    @Autowired
    private CommodityMapper commodityMapper ;

    /**
     * 用户下单
     * @param orderDTO
     * @return Result
     */
    @Override
    public void saveOrderInfo(OrderDTO orderDTO) {
        if (Objects.isNull(orderDTO)){
            throw new LogicException("下单信息不能为空！") ;
        }
        if (CollectionUtil.isEmpty(orderDTO.getOrderGoodsList())){
            throw new LogicException("下单商品不能为空！") ;
        }
        orderDTO.setOrderNo(UUID.randomUUID().toString().substring(10));
        orderDTO.setCreateTime(new Date());
        orderDTO.setStatus(OrderEnum.ORDER_CREATE.getCode());
        orderDTO.setUserId(userInfoContext.getUserId());
        orderDTO.setUserName(userInfoContext.getUser().getRealName());
        orderDTO.setUserPhone(userInfoContext.getUser().getPhone());
        /**保存订单信息*/
        Order order = new Order();
        BeanUtils.copyProperties(orderDTO,order);
        this.save(order) ;
        /**保存订单商品*/
        List<OrderGoods> list = orderDTO.getOrderGoodsList();
        for (OrderGoods goods : list) {
            /**门店商品库存 减*/
            commodityMapper.reduceShopCommodity(goods) ;
            goods.setOwnerShopId(order.getShopId());
            goods.setOwnerShopName(order.getShopName());
            goods.setOrderId(order.getId());
            /**保存订单商品信息*/
            goodsMapper.insert(goods) ;
        }
    }

    /**
     * 我的订单
     * @param id
     * @return
     */
    @Override
    public List<OrderDTO> getMyOrder(Long id) {
        List<OrderDTO> list = new ArrayList<>();
        /**用户下的所有订单*/
        LambdaQueryWrapper<Order> userOrderWrapper = new LambdaQueryWrapper();
        userOrderWrapper.eq(Order::getUserId,id) ;
        userOrderWrapper.orderByDesc(Order::getStatus) ;
        userOrderWrapper.orderByDesc(Order::getCreateTime) ;
        List<Order> orderList = orderMapper.selectList(userOrderWrapper);
        /**数组转换  order -> orderDTO*/
        for (Order order : orderList) {
            OrderDTO dto = new OrderDTO();
            BeanUtils.copyProperties(order,dto);
            list.add(dto) ;
        }
        /**存放order下所有的商品信息*/
        LambdaQueryWrapper<OrderGoods> goodsWrapper = new LambdaQueryWrapper<>();
        for (OrderDTO orderDTO : list) {
            /**查询订单下的orderGoods*/
            goodsWrapper.eq(OrderGoods::getOrderId,orderDTO.getId()) ;
            List<OrderGoods> goodsList = goodsMapper.selectList(goodsWrapper);
            if (CollectionUtil.isNotEmpty(goodsList)){
                for (OrderGoods goods : goodsList) {
                    orderDTO.getOrderGoodsList().add(goods) ;
                }
            }
        }
        return list;
    }

    /**
     * 订单详细信息
     * @Param id
     * @return OrderGoods
     */
    @Override
    public OrderGoods myOrderDetail(Long id) {
        return this.goodsMapper.selectById(id);
    }

    /**
     * 配送订单
     * @Param orderId
     */
    @Override
    public void sendOrder(Long orderId) {
        LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Order::getId,orderId) ;
        wrapper.set(Order::getStatus,OrderEnum.ORDER_CREATE_DESCRIBE.getCode()) ;
        this.update(wrapper) ;
    }

    /**
     * 已完成订单
     * @Param orderId
     */
    @Override
    public void successOrder(Long orderId) {
        LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Order::getId,orderId) ;
        wrapper.set(Order::getStatus,OrderEnum.ORDER_CREATE_DESCRIBE.getCode()) ;
        wrapper.set(Order::getCreateTime,new Date()) ;
        this.update(wrapper) ;
    }
}
